package com.ruoyi.notify.service.impl;

import com.aliyuncs.IAcsClient;
import com.aliyuncs.dm.model.v20151123.SingleSendMailRequest;
import com.aliyuncs.dm.model.v20151123.SingleSendMailResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.rabbitmq.client.Channel;
import com.ruoyi.common.core.domain.NotifyLogVo;
import com.ruoyi.common.rabbitmq.constant.RabbitmqConstants;
import com.ruoyi.common.rabbitmq.domain.EmailNotifyTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

@Service
public class EmailNotifyServiceImpl {

    @Autowired
    IAcsClient iAcsclient;

    @Value("${aliyun.emailSource}")
    String emailSource;

//    @Autowired
//    RemoteNotifyLogService remoteNotifyLogService;
//
    @RabbitListener(queues = {RabbitmqConstants.EMAIL_NOTIFY_QUEUE})
    @RabbitHandler
    public void EmailSendBatch(EmailNotifyTemplate emailNotifyTemplate, Message message, Channel channel) throws IOException {


        SingleSendMailRequest singleSendMailRequest =new SingleSendMailRequest();
        singleSendMailRequest.setAccountName(emailSource);
        singleSendMailRequest.setAddressType(1);
        singleSendMailRequest.setReplyToAddress(false);
        singleSendMailRequest.setSubject(emailNotifyTemplate.getSubject());
        singleSendMailRequest.setToAddress(emailNotifyTemplate.getTargetEmails());
        singleSendMailRequest.setActionName("SingleSendMail");
        singleSendMailRequest.setClickTrace("0");
        singleSendMailRequest.setFromAlias(emailNotifyTemplate.getFromAlias());
        singleSendMailRequest.setTextBody(emailNotifyTemplate.getTextBody());

        String errorCode = null;
        SingleSendMailResponse httpResponse = null;
        int notifyStatus = 1;
        try {

           httpResponse = iAcsclient.getAcsResponse(singleSendMailRequest);

        }  catch(ServerException e) {
            //捕获错误异常码
            System.out.println("ErrCode : " + e.getErrCode());
            errorCode = e.getErrCode();
            e.printStackTrace();

        } catch (ClientException e) {
            //捕获错误异常码
            System.out.println("ErrCode : " + e.getErrCode());
            errorCode = e.getErrCode();

            e.printStackTrace();
        }catch(Exception e){
            if(httpResponse == null){
                errorCode = "网络异常";
            }else{
                errorCode = "未知异常";
            }
             notifyStatus = 0;
        } finally {
            List<Long> notifyUserIds = emailNotifyTemplate.getNotifyUserId();
            List<String> notifyUserNames = emailNotifyTemplate.getNotifyUserName();
            for(int i = 0;i < notifyUserIds.size(); i++){
                NotifyLogVo vo = new NotifyLogVo();
                vo.setContent(emailNotifyTemplate.getTextBody());
                vo.setNotifyStatus(notifyStatus);
                vo.setNotifyTime(System.currentTimeMillis());
                vo.setNotifyUserId(notifyUserIds.get(i));
                vo.setNotifyUserName(notifyUserNames.get(i));
                vo.setNotifyType(2);
                vo.setRemark(errorCode);
                vo.setMessageId(Long.toString(message.getMessageProperties().getDeliveryTag()));
                List<NotifyLogVo>  vos = new LinkedList<>();
                vos.add(vo);
                try{
                   // remoteNotifyLogService.addNotify(vos);
                }catch (Exception e){
                    e.printStackTrace();
                }

            }

           // remoteNotifyLogService.addNotify(vos);
        }

    }

}
